package MicroRpc.framework.protocol.http;

import MicroRpc.framework.beans.Url;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
@Slf4j(topic = "m.HttpServer")
public class HttpServer {
    private static String CONTEXT_PATH="/tomcats";
    private static String SERVLET_NAME="DISPATCHERSERVLET";

    public static String getContextPath() {
        return CONTEXT_PATH;
    }


    public static void start(Url url){
        Tomcat tomcat=new Tomcat();
        tomcat.setPort(url.getPort());
        tomcat.getHost().setAutoDeploy(false);
        StandardContext context = new StandardContext();
        context.setPath(CONTEXT_PATH);
        context.addLifecycleListener(new Tomcat.FixContextListener());
        tomcat.getHost().addChild(context);

        tomcat.addServlet(CONTEXT_PATH,SERVLET_NAME,new DispatchServlet());
        context.addServletMappingDecoded("/*",SERVLET_NAME);
        try {
            tomcat.start();
            log.info("【tomcat 启动成功.】");
            tomcat.getServer().await();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

    }
}
